package it.unipr.ce.dsg.s2p.example.peerdroid;

import static npu.database.Constant.KEY_EMAIL;
import static npu.database.Constant.KEY_GIVENNAME;
import static npu.database.Constant.KEY_PHONE;
import static npu.database.Constant.KEY_ROWID;

import java.util.ArrayList;
import java.util.HashMap;


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;//delete when finish test

import npu.database.SingleData;
import npu.database.SQLiteAdapter;

public class Send extends Activity implements OnClickListener{
	
	private EditText edtNeighborTabSend, edtRequestUpdateTabSend, edtInviteUpdateTabSend;
	private TextView txtMyIPTabSend;
	private Button btnSendPingTabSend, btnRequestTabSend, btnInviteTabSend, btnPingBroadcast,
	 btnRequestBroadcast, btnInviteBroadcast, btnCloseTabSend;
//	private TextView contactAddressPeer;
//	private Button closeBut;
	
	private SQLiteAdapter mySQLiteAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.send);
		//database
		mySQLiteAdapter = new SQLiteAdapter(this);
		
		txtMyIPTabSend = (TextView)findViewById(R.id.txtMyIPTabSend);
		
		btnSendPingTabSend = (Button)findViewById(R.id.btnSendPingTagSend);
		btnRequestTabSend = (Button)findViewById(R.id.btnRequestTabSend);
		btnInviteTabSend = (Button)findViewById(R.id.btnInviteTabSend);
		btnPingBroadcast = (Button)findViewById(R.id.btnPingBroadcast);
		btnRequestBroadcast = (Button)findViewById(R.id.btnRequestBroadcast);
		btnInviteBroadcast = (Button)findViewById(R.id.btnInviteBroadcast);
		btnCloseTabSend = (Button)findViewById(R.id.btnCloseTabSend);
		
		edtNeighborTabSend =(EditText)findViewById(R.id.edtNeighborTagSend); 
		edtRequestUpdateTabSend =(EditText)findViewById(R.id.edtRequestUpdateTabSend); 
		edtInviteUpdateTabSend =(EditText)findViewById(R.id.edtInviteUpdateTabSend);
		
		if(PeerActivity.peer!=null){
			txtMyIPTabSend.setText(PeerActivity.peer.getAddressPeer().toString());//display IP
			
			btnSendPingTabSend.setOnClickListener(this);
			btnRequestTabSend.setOnClickListener(this);
			btnInviteTabSend.setOnClickListener(this);
			btnPingBroadcast.setOnClickListener(this);
			btnRequestBroadcast.setOnClickListener(this);
			btnInviteBroadcast.setOnClickListener(this);
			btnCloseTabSend.setOnClickListener(this);
			
		}//end if PeerActivity.peer !=null
	}//end onCreate
	
	 public void onClick(View v){
	        if(v instanceof Button){
	             switch(v.getId()){
	             case R.id.btnSendPingTagSend:{
	                  //do something
	            	 PeerActivity.peer.pingToPeer(edtNeighborTabSend.getText().toString());
//	            	 remember check validate before ping, v2
//	            	 Toast.makeText(this, edtNeighborTabSend.getText().toString() , Toast.LENGTH_LONG)
//	                 .show();
	                  break;
	             }
	             case R.id.btnRequestTabSend:{//for test, similar btninviteTabSend, but send many times, each time send 1 row
	            	//do something
	            	 
	            	 if(edtRequestUpdateTabSend.getText().toString().equals("")){
	            		 Toast.makeText(this, "You need input address" , Toast.LENGTH_LONG)
		                 .show();
	            	 }
	            	 else{
	            	 //testing, empty DataList > DatabasetoDataList > dataToPeer(address)
	            	 PeerActivity.peer.ClearDataList();
	            	 
	            	 //DatabaseToDataList
	                 mySQLiteAdapter.openToRead();

	                 Cursor cursor = mySQLiteAdapter.queueAll();
	                 startManagingCursor(cursor);
	                
	                 cursor.moveToFirst();
	               
	                 if (!cursor.isAfterLast())
	                 {
	                	 do
	                     {
	                		 SingleData single = new SingleData();
	                		 
	                        Long lObj = new Long(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ROWID)));
	                        String str = lObj.toString();//convert long to string
	                        single.setRowid(str);
//	                        note: in database, RowId has type is interger, but in other structure it has type String
//	                        single.setRowid  (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ROWID )));
	                        single.setVersion(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_VERSION)));
	                        single.setTypes(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_TYPES )));
	                        single.setGivenname(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_GIVENNAME )));
	                        single.setMiddlename(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_MIDDLENAME )));
	                        single.setFamilyname(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_FAMILYNAME )));
	                        single.setGender  (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_GENDER )));
	                        single.setSpinphone(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINPHONE )));
	                        single.setPhone(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_PHONE )));
	                        single.setSpinemail(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINEMAIL )));
	                        single.setEmail(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_EMAIL )));
	                        single.setSpinim(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINIM )));
	                        single.setIm(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_IM )));
	                        single.setSpinaddr(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINADDR )));
	                        single.setStreet(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_STREET )));
	                        single.setPobox(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_POBOX )));
	                        single.setCity(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_CITY )));
	                        single.setState(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_STATE )));
	                        single.setZipcode(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ZIPCODE )));
	                        single.setCountry(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_COUNTRY )));
	                        single.setSpinsns(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINSNS )));
	                        single.setSns(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SNS )));
	                        single.setSpinorg1(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINORG1 )));
	                        single.setOrg1(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ORG1 )));
	                        single.setSpinorg2(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINORG2 )));
	                        single.setOrg2(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ORG2 )));
	                        single.setNotes(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_NOTES )));
	                        single.setTime(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_TIME )));
	                        single.setPhoto (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_PHOTO)));

//	                        PeerActivity.peer.addDataList(single); 
	                      //Send singleData
	   	                     PeerActivity.peer.singleRowToPeer(edtRequestUpdateTabSend.getText().toString(),single);
	   		            
	                        
	                     }
	                     while (cursor.moveToNext());
	                             
	                 }
	                 
	                 
	                 mySQLiteAdapter.close();
	            	 
	            	 	
	            	 }
	            	 
	            	break;
	             }
	             case R.id.btnInviteTabSend:{//send whole database at one time, but problem is byte< 512
		            //check address
	            	 if(edtInviteUpdateTabSend.getText().toString().equals("")){
	            		 Toast.makeText(this, "You need input address" , Toast.LENGTH_LONG)
		                 .show();
	            	 }
	            	 else{
	            	 //testing, empty DataList > DatabasetoDataList > dataToPeer(address)
	            	 PeerActivity.peer.ClearDataList();
	            	 
	            	 //DatabaseToDataList
	                 mySQLiteAdapter.openToRead();

	                 Cursor cursor = mySQLiteAdapter.queueAll();
	                 startManagingCursor(cursor);
	                
	                 cursor.moveToFirst();
	               
	                 if (!cursor.isAfterLast())
	                 {
	                	 do
	                     {
	                		 SingleData single = new SingleData();
	                		 
	                        Long lObj = new Long(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ROWID)));
	                        String str = lObj.toString();//convert long to string
	                        single.setRowid(str);
//	                        note: in database, RowId has type is interger, but in other structure it has type String
//	                        single.setRowid  (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ROWID )));
	                        single.setVersion(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_VERSION)));
	                        single.setTypes(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_TYPES )));
	                        single.setGivenname(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_GIVENNAME )));
	                        single.setMiddlename(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_MIDDLENAME )));
	                        single.setFamilyname(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_FAMILYNAME )));
	                        single.setGender  (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_GENDER )));
	                        single.setSpinphone(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINPHONE )));
	                        single.setPhone(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_PHONE )));
	                        single.setSpinemail(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINEMAIL )));
	                        single.setEmail(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_EMAIL )));
	                        single.setSpinim(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINIM )));
	                        single.setIm(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_IM )));
	                        single.setSpinaddr(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINADDR )));
	                        single.setStreet(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_STREET )));
	                        single.setPobox(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_POBOX )));
	                        single.setCity(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_CITY )));
	                        single.setState(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_STATE )));
	                        single.setZipcode(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ZIPCODE )));
	                        single.setCountry(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_COUNTRY )));
	                        single.setSpinsns(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINSNS )));
	                        single.setSns(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SNS )));
	                        single.setSpinorg1(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINORG1 )));
	                        single.setOrg1(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ORG1 )));
	                        single.setSpinorg2(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_SPINORG2 )));
	                        single.setOrg2(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_ORG2 )));
	                        single.setNotes(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_NOTES )));
	                        single.setTime(cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_TIME )));
	                        single.setPhoto (cursor.getString(cursor.getColumnIndexOrThrow(mySQLiteAdapter.KEY_PHOTO)));

	                        PeerActivity.peer.addDataList(single); 
	                        
	                        
	                     }
	                     while (cursor.moveToNext());
	                             
	                 }
	                 
	                 
	                 mySQLiteAdapter.close();
	            	 
	            	 //dataToPeer
	                 PeerActivity.peer.dataToPeer(edtInviteUpdateTabSend.getText().toString());
		            	
	            	 }
	            	 break;
		          }
	             case R.id.btnPingBroadcast:{
		            	//do something
		            	break;
		          }
	             case R.id.btnRequestBroadcast:{
		            	//do something
		            	break;
		          }
	             case R.id.btnInviteBroadcast:{
		            	//do something
		            	break;
		          }
	             case R.id.btnCloseTabSend:{
		            	//do something
		            	break;
		          }
	             }//end switch
	        }// end if -instance of Button
	        
	       }//end onClick
	
}

